
package com.jf.cloud.product.feign;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jf.cloud.api.multishop.bo.EsShopDetailBO;
import com.jf.cloud.api.multishop.constant.ShopStatus;
import com.jf.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.jf.cloud.api.product.feign.ProductFeignClient;
import com.jf.cloud.api.supplier.bo.SupplierDetailBO;
import com.jf.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.product.bo.EsProductBO;
import com.jf.cloud.common.product.constant.SupplierSpuType;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.product.service.CategoryService;
import com.jf.cloud.product.service.SpuEsSearchService;
import com.jf.cloud.product.service.SpuService;
import com.jf.cloud.product.service.SpuTagReferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/11/27
 */
@RestController
@Hidden
public class ProductFeignController implements ProductFeignClient {

    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuEsSearchService spuEsSearchService;
    @Autowired
    private ShopDetailFeignClient shopDetailFeignClient;
    @Autowired
    private SpuTagReferenceService spuTagReferenceService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SupplierDetailFeignClient supplierDetailFeignClient;

    @Override
    public ServerResponseEntity<EsProductBO> loadEsProductBO(Long spuId) {
        EsProductBO esProductBO = spuEsSearchService.loadEsProductBO(spuId);
        List<Long> spuTagIds = spuTagReferenceService.tagListBySpuId(spuId);
        if (CollUtil.isNotEmpty(spuTagIds)) {
            esProductBO.setTagIds(spuTagIds);
        }
        //如果是供应商品 获取供应商名称
        if (Objects.equals(esProductBO.getSupplierSpuType(), SupplierSpuType.YES.value())) {
            ServerResponseEntity<SupplierDetailBO> supplierEntity = supplierDetailFeignClient.getSupplierBySupplierId(esProductBO.getSupplierId());
            if (supplierEntity.isFail()) {
                throw new LuckException(supplierEntity.getMsg());
            }
            SupplierDetailBO supplier = supplierEntity.getData();
            esProductBO.setSupplierName(supplier.getSupplierName());
        } else {
            // 获取店铺信息
            if (!Objects.equals(esProductBO.getShopId(), Constant.ZERO_LONG)) {
                ServerResponseEntity<EsShopDetailBO> shopDetailResponse = shopDetailFeignClient.getShopByShopId(esProductBO.getShopId());
                if (shopDetailResponse.isFail()) {
                    throw new LuckException(shopDetailResponse.getMsg());
                }
                EsShopDetailBO shopDetail = shopDetailResponse.getData();
                if (esProductBO.getAppDisplay() && !Objects.equals(shopDetail.getShopStatus(), ShopStatus.OPEN.value())) {
                    esProductBO.setAppDisplay(Boolean.FALSE);
                }
                esProductBO.setShopName(shopDetail.getShopName());
                esProductBO.setShopAddress(shopDetail.getProvince()+shopDetail.getCity()+shopDetail.getArea());
                if(!ObjectUtil.isEmpty(shopDetail.getAreaId())) {
                    esProductBO.setAreaCode(shopDetail.getAreaId().toString());
                }
                esProductBO.setShopImg(shopDetail.getShopLogo());
                esProductBO.setShopType(shopDetail.getType());
            }
        }
        if (Objects.isNull(esProductBO.getCommentNum())) {
            esProductBO.setCommentNum(0);
        }
        if (Objects.isNull(esProductBO.getSaleNum())) {
            esProductBO.setSaleNum(0);
        }
        if (CollUtil.isEmpty(esProductBO.getPartyCodes())) {
            esProductBO.setPartyCodes(null);
        }
        if (CollUtil.isEmpty(esProductBO.getModelIds())) {
            esProductBO.setModelIds(null);
        }
        return ServerResponseEntity.success(esProductBO);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByShopCategoryIds(List<Long> shopCategoryIds) {
        return getSpuIdsBySpuUpdateDTO(shopCategoryIds, null, null, null);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByCategoryIds(List<Long> categoryIds) {
        return getSpuIdsBySpuUpdateDTO(null, categoryIds, null, null);
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuIdsByBrandId(Long brandId) {
        return getSpuIdsBySpuUpdateDTO(null, null, brandId, null);
    }


    @Override
    public ServerResponseEntity<List<SpuVO>> listSpuIdsBySupplierIdAndSupplierSpuType(Long supplierId, String createTime, Integer supplierSpuType) {
        Date spuCreateTime = Objects.nonNull(createTime) ? DateUtil.parse(createTime, "yyyy-MM-dd") : null;
        return ServerResponseEntity.success(spuService.getSpuIdsBySupplierIdAndSupplierSpuType(supplierId, spuCreateTime, supplierSpuType));
    }

    @Override
    public ServerResponseEntity<List<SpuVO>> listMerchantSpuIdsBySupplierId(Long supplierId) {
        return ServerResponseEntity.success(spuService.listMerchantSpuIdsBySupplierId(supplierId));
    }
    @Override
    public ServerResponseEntity<List<SpuVO>> listSpuBySpuIds(List<Long> spuIds) {
        return ServerResponseEntity.success(spuService.listSpuDetailBySpuIds(spuIds));
    }

    @Override
    public ServerResponseEntity<List<Long>> getSpuTagBySpuId(Long spuId) {
        return ServerResponseEntity.success(spuTagReferenceService.tagListBySpuId(spuId));
    }

    @Override
    public ServerResponseEntity<Void> handleStatusChange(Long spuId) {
        spuService.handleStatusChange(Collections.singletonList(spuId));
        return ServerResponseEntity.success();
    }

//    @Override
//    public ServerResponseEntity<List<ProdEffectRespVO>> getSpuListByParam(ProdEffectDTO param, Set<Long> spuIds) {
//        return ServerResponseEntity.success(spuService.getSpuListByParam(param,spuIds));
//    }
//
//    @Override
//    public ServerResponseEntity<Long> getSpuListTotal(ProdEffectDTO param, Set<Long> spuIds) {
//        return ServerResponseEntity.success(spuService.getSpuListTotal(param,spuIds));
//    }

    /**
     * 获取spuId列表
     *
     * @param shopCategoryIds 店铺分类id列表
     * @param categoryIds     平台分类Id列表
     * @param brandId         品牌id
     * @param shopId          店铺id
     * @return
     */
    public ServerResponseEntity<List<Long>> getSpuIdsBySpuUpdateDTO(List<Long> shopCategoryIds, List<Long> categoryIds, Long brandId, Long shopId) {
        List<Long> spuIds = spuService.getSpuIdsBySpuUpdateDTO(shopCategoryIds, categoryIds, brandId, shopId);
        return ServerResponseEntity.success(spuIds);
    }
}
